package GUI; //done

import java.awt.Rectangle;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Entities.DealSale;

/**
 * This Class controls DealSale Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class DealSaleWindow extends TemplateWindow{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The active ID Label. */
	private JLabel activeIDjLabel = null;
	
	/** The active ID Label. */
	private JLabel startDatejLabel = null;
	
	/** The end Date jLabel. */
	private JLabel endDatejLabel = null;
	
	/** The message jLabel. */
	private JLabel messagejLabel = null;
	
	/** The Dealsale Text Field. */
	private JTextField DealsaleID = null;
	
	/** The Start Date Text Field. */
	private JTextField startDatejTextField = null;
	
	/** The End Date Text Field. */
	private JTextField endDatejTextField = null;
	
	/** The status JComboBox. */
	private JComboBox statusJComboBox = null;
	
	/** The next jButton. */
	private JButton nextjButton = null;
	
	/** The update A jButton. */
	private JButton updateAjButton = null;
	
	/** The Start Date. */
	private Date startDate = null;
	
	/** The Start Date. */
	private Date endDate = null;
	
	/** The Current Date. */
	private DateFormat dateFormat = null;
	
	/** The DealSale. */
	private DealSale dealsale = null;
	
	/**
	 * The default constructor 
	 */
	public DealSaleWindow(){
		super();
		initialize();
	}
	
	/**
	 * This function initializes DealSaleWindow
	 */
	private void initialize(){
		dealsale = new DealSale(0,null,null,null,null);
		activeIDjLabel = new JLabel();
		activeIDjLabel.setBounds(new Rectangle(17, 248, 107, 23));
		activeIDjLabel.setText("DealSaleID");
		
		startDatejLabel = new JLabel();
		startDatejLabel.setBounds(new Rectangle(17, 270, 107, 23));
		startDatejLabel.setText("Start date");
		
		endDatejLabel = new JLabel();
		endDatejLabel.setBounds(new Rectangle(17, 290, 107, 23));
		endDatejLabel.setText("End date");
		
		messagejLabel = new JLabel();
		messagejLabel.setBounds(new Rectangle(202, 259, 72, 23));
		messagejLabel.setText("Status :");
		
		startDate = new Date();
		endDate = new Date();
		dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		
		this.add(activeIDjLabel, null);
		this.add(startDatejLabel, null);
		this.add(endDatejLabel, null);
		this.add(messagejLabel, null);
		
		this.add(getActiveIDjTextField(), null);
		this.add(getStartDatejTextField(), null);
		this.add(getEndDatejTextField(), null);
		this.add(getStatusJTextField() , null);
		
		super.closeall();
	}	
	
	/**
	 * @return the dealsale
	 */
	public DealSale getDealSale(){
		return dealsale;
	}

	/**
	 * @param dealsale the dealsale to set
	 */
	public void setDealSale(DealSale dealsale){
		this.dealsale = dealsale;
	}

	/**
	 * This function initializes DealsaleID		
	 * @return javax.swing.JTextField	
	 */
	private JTextField getActiveIDjTextField(){
		if (DealsaleID == null) {
			DealsaleID = new JTextField();
			DealsaleID.setBounds(new Rectangle(85, 248, 107, 23));			
		}
		return DealsaleID;
	}
	
	/**
	 * This function initializes startDatejTextField		
	 * @return javax.swing.JTextField	
	 */
	private JTextField getStartDatejTextField() {
		if (startDatejTextField == null) {
			startDatejTextField = new JTextField();
			startDatejTextField.setBounds(new Rectangle(85, 270, 107, 23));
		}
		return startDatejTextField;
	}
	
	/**
	 * This function initializes endDatejTextField		
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEndDatejTextField() {
		if (endDatejTextField == null) {
			endDatejTextField = new JTextField();
			endDatejTextField.setBounds(new Rectangle(85, 290, 107, 23));
			endDatejTextField.setText("yyyy/MM/dd");
		}
		return endDatejTextField;
	}
	
	/**
	 * This function initializes statusJTextField	
	 * @return java.awt.TextArea
	 */
	private JComboBox getStatusJTextField() {
		if (statusJComboBox == null) {
			statusJComboBox = new JComboBox();
			statusJComboBox.setBounds(new Rectangle(248, 259, 60, 23));
			statusJComboBox.addItem("Run");
			statusJComboBox.addItem("Stop");
			statusJComboBox.addItem("End");
		}
		return statusJComboBox;
	}
	
	/**
	 * This function initializes nextjButton		
	 * @return javax.swing.JButton	
	 */
	public JButton getNextjButton(){
		if (nextjButton == null){
			nextjButton = new JButton();
			nextjButton.setBounds(new Rectangle(380, 20, 100, 25));
			nextjButton.setText("Next>>");
			nextjButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){
					dealsale.setStatus((String)statusJComboBox.getSelectedItem());
					try {
						String msg = dealsale.saveDealSale();
						if (msg != null){
							if (msg.equals("OK"))
								JOptionPane.showMessageDialog(null, "DealSale successfuly saved");
							else
								JOptionPane.showMessageDialog(null, "DealSale not saved!","",JOptionPane.WARNING_MESSAGE);
						}
					} 
					catch (Exception e1){
						JOptionPane.showMessageDialog(null, "ERROR!","",JOptionPane.ERROR_MESSAGE);
					}
				}
			});	
		}
		return nextjButton;
	}
	
	/**
	 * @return boolean
	 */
	public boolean ifNext(){
		Date sdate, edate;
		long id;
		String status;
		try{
			sdate = convertStringToDate(startDatejTextField.getText());
			if (sdate == null){
				JOptionPane.showMessageDialog(null, "Ilegal start date systax!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if (startDate.getYear() > sdate.getYear()){	
				System.out.println(sdate);
				JOptionPane.showMessageDialog(null, "Ilegal start date!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			else
				if (startDate.getYear() == sdate.getYear()){	
					if (startDate.getMonth() > sdate.getMonth()){	
						System.out.println(sdate);
						JOptionPane.showMessageDialog(null, "Ilegal start date!","",JOptionPane.ERROR_MESSAGE);
						return false;
					}
					else
						if (startDate.getMonth() == sdate.getMonth()){	
							if (startDate.getDay() > sdate.getDay()){	
								System.out.println(sdate);
								JOptionPane.showMessageDialog(null, "Ilegal start date!","",JOptionPane.ERROR_MESSAGE);
								return false;
							}
						}
				}
			
			edate = convertStringToDate(endDatejTextField.getText());
			if (edate == null){
				JOptionPane.showMessageDialog(null, "Ilegal end date systax!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if (sdate.after(edate)){	
					JOptionPane.showMessageDialog(null, "Ilegal end date!","",JOptionPane.ERROR_MESSAGE);
					return false;
			}
			id = Long.parseLong(DealsaleID.getText());
			if (id > 0){
				status = (String)statusJComboBox.getSelectedItem();
				dealsale.setDealSaleID(id);
				dealsale.setStartDate(sdate);
				dealsale.setEndDate(edate);
				dealsale.setStatus(status);
				return true;
			}
			else
				JOptionPane.showMessageDialog(null, "Ilegal ID!","",JOptionPane.WARNING_MESSAGE);
			return false;
		}
		catch(Exception exc){
			JOptionPane.showMessageDialog(null, "Ilegal input!","",JOptionPane.WARNING_MESSAGE);
			return false;
		}
	}
	
	/**
	 * This function converts string ("yyyy/MM/dd") to date
	 * @param datestr the string to convert
	 * @return Date on success, null on fail
	 */
	private Date convertStringToDate(String datestr){
		String year,month, day;
		try{
			year = datestr.substring(0,datestr.indexOf("/"));
			datestr = datestr.substring(datestr.indexOf("/")+1, datestr.length());
			month = datestr.substring(0,datestr.indexOf("/"));
			datestr = datestr.substring(datestr.indexOf("/")+1, datestr.length());
			day = datestr;
			Date sdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			return sdate;
		}
		catch(Exception exc){
			return null;
		}
	}
	
	/**
	 * This function load dealsale to the window
	 * @param dealsale the dealsale to load
	 */
	public void loadDealSale(DealSale dealsale){
		this.dealsale = dealsale;
		super.loadTemplate(dealsale.getTemplate());
		String status = dealsale.getStatus();
		if (status != null)		
			statusJComboBox.setSelectedItem(status);
		else
			statusJComboBox.setSelectedItem("Run");
		if (dealsale.getStartDate() != null)
			startDatejTextField.setText(dateFormat.format(dealsale.getStartDate()));
		else
			startDatejTextField.setText("");
		if (dealsale.getEndDate() != null)
			endDatejTextField.setText(dateFormat.format(dealsale.getEndDate()));
		else
			endDatejTextField.setText("yyyy/MM/dd");
		if (dealsale.getDealSaleID() > 0)
			DealsaleID.setText(String.valueOf(dealsale.getDealSaleID()));
		else
			DealsaleID.setText("");
	}
	
	/**
	 * This function add next button to the window
	 */
	public void addNextjButton(){
		this.add(getNextjButton(), null);
		startDatejTextField.setText(dateFormat.format(startDate));
	}
	
	/**
	 * This function initializes updatejButton	
	 * @return javax.swing.JButton	
	 */
	public JButton getAUpdatejButton() {
		if (updateAjButton == null){
			updateAjButton = new JButton();
			updateAjButton.setBounds(new Rectangle(260, 400, 100, 25));
			updateAjButton.setText("Update");
			updateAjButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dealsale.setStatus((String)statusJComboBox.getSelectedItem());
					try {
						String msg = dealsale.updateDealSale();
						if (msg != null){
							if (msg.equals("OK"))
								JOptionPane.showMessageDialog(null, "Successfuly update");
							else
								JOptionPane.showMessageDialog(null, "Not update!","",JOptionPane.WARNING_MESSAGE);
						}
					} 
					catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "ERROR!","",JOptionPane.ERROR_MESSAGE);
					}
				}
			});	
		}
		return updateAjButton;
	}
	
	/**
	 * This function add UpdatejButton to window
	 */
	public void addUpdatejButton(){
		DealsaleID.setEditable(false);
		startDatejTextField.setEditable(false);
		endDatejTextField.setEditable(false);
		this.add(getAUpdatejButton() , null);		
	}
}
